package com.tangcco.lockwell.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Article;
import com.tangcco.lockwell.domain.DataDictionary;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.service.ArticleService;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.viewmodel.admin.question.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
/*薛明宇*/
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tangcco.lockwell.utility.DateToString.dateToString;
import static com.tangcco.lockwell.utility.JsonUtil.toJsonStr;

@RestController("QuestionController")
@RequestMapping(value = "/api/admin/question")
public class QuestionController {


    private final QuestionService questionService;
    private final ArticleService articleService;

    @Autowired
    public QuestionController(QuestionService questionService, ArticleService articleService) {
        this.questionService = questionService;
        this.articleService = articleService;
    }

    @Autowired
    protected WebContext webContext;


    //分页查询
    @RequestMapping("/page")
    public RestResponse<PageInfo<QuestionVm>> pageList(@RequestBody QuestionPageRequestVM questionPageRequestVM) {
        // 开始分页
        PageHelper.startPage(questionPageRequestVM.getPageIndex(), questionPageRequestVM.getPageSize());
        // 获取分页结果
        List<QuestionVm> questionVms = questionService.selectAll(questionPageRequestVM);
        // 将 Page 转换为 PageInfo
        PageInfo<QuestionVm> pageInfo = new PageInfo<>(questionVms);

        return RestResponse.ok(pageInfo);
    }

    //增加和修改
    @Transactional
    @RequestMapping("/edit")
    public RestResponse insert(@RequestBody @Valid QuestionVMUpdate model) {
        if (model.getId() == null) {
            if (model.getArticleContent() != "" && model.getArticleContent() != null) {
                Article article = new Article();
                article.setArticleContent(model.getArticleContent());
                article.setCreateTime(dateToString(new Date()));
                articleService.insert(article);
                model.setArticleId(article.getArticleId());
            }
            model.setCreateUserId(webContext.getCurrentUser().getUserId());
            model.setTextContent(toJsonStr(model.getTextContent()));
            model.setCreateTime(dateToString(new Date()));
            model.setModifyTime(dateToString(new Date()));
            questionService.insertQuestion(model);
        } else {
            if (model.getArticleContent() != null) {
                Article article = new Article();
                article.setArticleId(model.getArticleId());
                article.setArticleContent(model.getArticleContent());
                article.setModifyTime(dateToString(new Date()));
                articleService.updateById(article);
            }
            model.setModifyTime(dateToString(new Date()));
            model.setTextContent(toJsonStr(model.getTextContent()));
            questionService.updateByIdS(model);
        }
        return RestResponse.ok(model.getId());
    }

    //查询所有
    @RequestMapping(value = "/select/{id}")
    public RestResponse select(@PathVariable Integer id) {
        QuestionVm questionVm = questionService.selectById(id);
        return RestResponse.ok(questionVm);
    }


    //删除
    @RequestMapping("/delete/{id}")
    public RestResponse deleteById(@PathVariable Integer id) {
        questionService.deleteById(id);
        return RestResponse.ok();
    }

    //查询question数量
    @RequestMapping("/selectCount")
    public RestResponse selectCount() {
        int count = questionService.selectCount();
        return RestResponse.ok(count);
    }

    //根据文章id查询
    @RequestMapping("/selectByArticleId/{id}")
    public RestResponse selectByArticleId(@PathVariable Integer id) {
        Article article = articleService.selectById(id);
        return RestResponse.ok(article);
    }

    //添加和修改文章
    @RequestMapping("/addArticle")
    public RestResponse addArticle(@RequestBody @Valid Article model) {
        if (model.getArticleId() == null) {
            model.setCreateTime(dateToString(new Date()));
            articleService.insert(model);
        } else {

            model.setModifyTime(dateToString(new Date()));
            articleService.updateByIdFilter(model);
        }
        return RestResponse.ok(model.getArticleId());
    }

    //删除文章
    @RequestMapping("/deleteArticle/{id}")
    public RestResponse deleteArticle(@PathVariable Integer id) {
        articleService.deleteById(id);
        return RestResponse.ok();
    }


    //分页查询试卷中没有的题目
    @RequestMapping("/pageOther")
    public RestResponse<PageInfo<QuestionVm>> pageListOther(@RequestBody QuestionPageRequestOtherVM questionPageRequestOtherVM) {
        // 开始分页
        PageHelper.startPage(questionPageRequestOtherVM.getPageIndex(), questionPageRequestOtherVM.getPageSize());
        List<Integer> list = new ArrayList<>();
        List<AlerdyQuestion> alerdyItems = questionPageRequestOtherVM.getAlerdyItems();
        for (AlerdyQuestion testQuestion : alerdyItems) {
            QuestionPageRequestVM[] questionItems = testQuestion.getQuestionItems();
            for (QuestionPageRequestVM q1 : questionItems) {
                list.add(q1.getId());
            }
        }
        questionPageRequestOtherVM.setAlerdyIds(list);
        // 获取分页结果
        List<QuestionVm> questionVms = questionService.selectAllOther(questionPageRequestOtherVM);

        // 将 Page 转换为 PageInfo
        PageInfo<QuestionVm> pageInfo = new PageInfo<>(questionVms);

        return RestResponse.ok(pageInfo);
    }

    //按照指定格式查询试卷表所需题目
    @RequestMapping("/selectQuestionById/{id}")
    public RestResponse selectQuestionById(@PathVariable Integer id) {
        QuestionEditRequestVM questionEditRequestVM = questionService.selectQuestionById(id);
        return RestResponse.ok(questionEditRequestVM);
    }

}



